package com.dys.service.impl;

import com.dys.dto.UserDTO;
import com.dys.mapper.UserMapper;
import com.dys.mapper.UserRoleMapper;
import com.dys.pojo.User;
import com.dys.pojo.UserExample;
import com.dys.pojo.UserRoleExample;
import com.dys.pojo.UserRoleKey;
import com.dys.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @className: com.dys.service.impl.UserServiceImpl
 * @description: 用户管理
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/11/14 21:25
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<User> listUserInfoByUser(User user) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (user != null) {
            if (user.getUserName() != null && !"".equals(user.getUserName())) {
                criteria.andUserNameEqualTo(user.getUserName());
            }
        }
        // 查询u2不为1的记录
        criteria.andU2NotEqualTo("1");
        return userMapper.selectByExample(userExample);
    }

    @Override
    public int addUserInfo(User user) throws Exception {
        return userMapper.insertSelective(user);
    }

    @Override
    public int updateUserInfo(User user) throws Exception {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int deleteUserInfo(int userId) throws Exception {
        return userMapper.deleteByPrimaryKey(userId);
    }

    @Override
    public int saveUserInfo(UserDTO userDTO) throws Exception {
        // 1.添加用户信息
        User user = userDTO.getUser();

        if (user.getUserId() != null) {
            // 更新用户信息
            this.updateUserInfo(user);
        } else {
            // 需要对密码加密 MD5加密+salt(盐值)
            String salt = UUID.randomUUID().toString();
            SimpleHash passwordHash = new SimpleHash("MD5", user.getPassword(), salt, 1024);
            user.setPassword(passwordHash.toHex());
            user.setU1(salt);
            user.setU2("0");
            this.addUserInfo(user);
        }

        // 添加之前：需要根据用户编号删除对应的角色信息
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(user.getUserId());
        userRoleMapper.deleteByExample(userRoleExample);

        // 2.分配用户和角色的关联关系
        // 获取分配给当前用户的角色信息
        List<Integer> roleIds = userDTO.getRoleIds();
        if (roleIds != null && roleIds.size() > 0) {
            // 分配所有角色信息
            for (Integer roleId : roleIds) {
                // 将用户和角色的关联关系保存到 t_user_role 中
                UserRoleKey userRole = new UserRoleKey();
                userRole.setUserId(user.getUserId());
                userRole.setRoleId(roleId);
                userRoleMapper.insertSelective(userRole);
            }
        }
        return 1;
    }

    @Override
    public User findUserInfoByUserId(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public List<Integer> findUserRoleIdsByUserId(int userId) {
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(userId);
        List<UserRoleKey> userRoleKeys = userRoleMapper.selectByExample(userRoleExample);
        List<Integer> roleIds = new ArrayList<>();
        for (UserRoleKey userRoleKey : userRoleKeys) {
            roleIds.add(userRoleKey.getRoleId());
        }
        return roleIds;
    }

    @Override
    public int deleteUserInfoByUserId(int userId) {
        User user = new User();
        user.setUserId(userId);
        user.setU2("1");
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public PageInfo<User> findUserInfoByPage(UserDTO userDTO) throws Exception {
        PageHelper.startPage(userDTO.getPageNum(), userDTO.getPageSize());
        List<User> list = this.listUserInfoByUser(userDTO.getUser());
        PageInfo<User> pageInfo = new PageInfo<>(list);
        list.stream().forEach(System.out::println);
        return pageInfo;
    }
}
